import { defineStore } from 'pinia'
import { getlistApi, postlistApi, deleteApi, postUpdatelistApi, getNumberApi } from '@/api/table'

export const TableStore = defineStore('tableStore', {
	state: () => {
		return {
			list: [],
			total: '',
			by: undefined,
			order: 'ascend',
			page: 1,
			pageSize: 5,
		}
	},
	getters: {

	},
	actions: {
		async SaveList(form) {
			if (form._id) {
				await postUpdatelistApi(form)
			} else {
				await postlistApi(form)
			}

		},
		async getList(row: Object) {
			console.log(row);
			const { query, query1, query2, page, pageSize } = row
			const { data, total } = await getlistApi({
				query: query,
				query1: query1,
				query2: query2,
				page: this.page - 1,
				pageSize: this.pageSize
			})
			this.list = data
			this.total = total
		},
		async Delete(_id: string) {
			await deleteApi({ _id })
		},
		async getNumber(number: string) {
			return await getNumberApi({ number })
		}
	},
})